// @deno-types="https://unpkg.com/@types/pinyin@2.8.2/index.d.ts"
import pinyin from 'https://jspm.dev/pinyin';
// @deno-types="./lodash.d.ts"
import lodash from 'https://jspm.dev/lodash';
import { parse } from "https://deno.land/std/flags/mod.ts";

import json2json from './json2json.ts';

json2json((json:Array<Array<any>>)=>{
    const args = parse(Deno.args);
    const {path='.', tofield='pinyin', rows='.'}= args;
    function getByPath(o:any,path:string) {
        return path=='.'?o:lodash.get(o,path);
    }
    getByPath(json, rows).forEach((row:any)=>{
        const obj = getByPath(row, path);
        var pinyinresult = pinyin(obj[args.textfield],{style: pinyin.STYLE_TONE2,segment:false});
        const pinyinr = lodash.flatten(pinyinresult).join('').replace(/[^\x00-\x7F]/g, "");
        lodash.set(obj, tofield, pinyinr);
    })
    return json;
})
